#include "ASScrMain.h"
#include "ASScreenMgr.h"
#include "..\app\asappimginfo.h"
#include "..\control\ASAdvbar.h"
#include "ASScrCategory.h"
#include "ASScrList.h"
#include "ASScrDetail.h"
#include "mrc_advbar.h"
#include "ASWaitingScreen.h"
#include "res_str.h"
#include "ASScreenShare.h"
#include "..\app\ASAppConst.h"
#include "..\app\ASLogic.h"
#include "..\app\ASApp.h"
#include "ASConfirmScreen.h"
#include "ASScrProcess.h"
#include "ASScrSetting.h"
#include "mrc_base.h"
#include "mrc_exb.h"

static as_main_screen* s_main_screen_p;
static as_main_bt	s_btninfo[ENUM_CATEGORY_COUNT];
static as_button* s_btns[ENUM_CATEGORY_COUNT];
static ASINT s_cur_category;


static void as_scr_main_btn_category(void)
{
    // as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    //scr_mgr->delete_and_pop_screen();
    as_show_waiting_screen(TEXT_WAITING);
    as_scr_category_show();
}

static void as_scr_main_btn_all(void)
{
    //as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    //scr_mgr->delete_and_pop_screen();
    as_show_waiting_screen(TEXT_WAITING);
    as_scr_list_show(ENUM_RECORD_ALL);
}
static void as_scr_main_btn_favorite(void)
{
    //as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    //scr_mgr->delete_and_pop_screen();
    as_show_waiting_screen(TEXT_WAITING);
    as_scr_list_show(ENUM_RECORD_FAVORITE);
}
static void as_scr_main_btn_todo(void)
{
    //as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    //scr_mgr->delete_and_pop_screen();
    as_show_waiting_screen(TEXT_WAITING);
    as_scr_list_show(ENUM_RECORD_TODO);
}
static void as_scr_main_btn_untried(void)
{
    //as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    //scr_mgr->delete_and_pop_screen();
    as_show_waiting_screen(TEXT_WAITING);
    as_scr_list_show(ENUM_RECORD_TRIED);
}
static void as_scr_main_btn_tried(void)
{
    //as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    //scr_mgr->delete_and_pop_screen();
    as_show_waiting_screen(TEXT_WAITING);
    as_scr_list_show(ENUM_RECORD_UNTRIED);
}
static void as_scr_main_btn_mood(void)
{
    ASINT icate = 0;
    mrc_sand(mrc_getUptime());
    icate = mrc_rand() % 106;
    as_scr_detail_show(icate, ENUM_RECORD_RANDOM);
}
static void as_scr_main_btn_recent(void)
{
    //as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    //scr_mgr->delete_and_pop_screen();
    as_show_waiting_screen(TEXT_WAITING);
    as_scr_list_show(ENUM_RECORD_RECENT);
}
static void as_scr_main_btn_random(void)
{
    ASINT icate = 0;
    mrc_sand(mrc_getUptime());
    icate = mrc_rand() % 106;
    as_scr_detail_show(icate, ENUM_RECORD_RANDOM);
}

static void as_scr_main_entry_process(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    scr_mgr->delete_and_pop_screen();
    as_scr_process_show();
}

static void as_scr_main_entry_setting(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    scr_mgr->delete_and_pop_screen();
    as_scr_setting_show();
}
static void as_scr_main_tab_evt(ASINT idx)
{
    switch(idx)
    {
        case 0:
            break;

        case 1:
            as_scr_main_entry_process();
            break;

        case 2:
            as_scr_main_entry_setting();
            break;
    }
}

static void as_scr_ascii_to_ucs2(const ASSTR src1, const ASSTR src2, ASWSTR dst1, ASWSTR dst2, ASINT dst1len, ASINT dst2len)
{
    as_memset(dst1, 0, dst1len);
    as_memset(dst2, 0, dst2len);
    as_ascii_to_ucs2(dst1, src1);
    as_ascii_to_ucs2(dst2, src2);
}
static void as_scr_add_head(as_screen* screen_p)
{
    as_rect area;
    ASINT w, h, strh;
    ASUINT param[6];
    ASWCHAR tmp[30], tmp2[30];
    as_tablebar* tab_p;
    as_get_internal_img_res_size(IMG_TAB_VIEW, &w, &h);
    strh = as_get_font_height();
    h = h > strh ? h : strh;
    s_main_screen_p->head_height = h + as_app_get_advbar_height();
    area.w = as_get_lcd_width();
    area.h = (ASINT16)h;
    area.x = 0;
    area.y = as_app_get_advbar_height();
    param[0] = 3;
    param[1] = AS_TABLEBAR_IMAGE_TYPE;
    param[2] = AS_TABLEBAR_SHOW_HORIZONTAL;
    tab_p = (as_tablebar*)screen_p->add_ctrl(screen_p, AS_CTRL_TABLEBAR, area, 3, (void**)param);
    as_scr_ascii_to_ucs2(IMG_TAB_VIEW, IMG_TAB_VIEW_D, tmp, tmp2, 30, 30);
    tab_p->add_item((as_control*)tab_p, tmp, tmp2, NULL, AS_IMAGE_FROM_RES);
    as_scr_ascii_to_ucs2(IMG_TAB_PROCESS, IMG_TAB_PROCESS_D, tmp, tmp2, 30, 30);
    tab_p->add_item((as_control*)tab_p, tmp, tmp2, NULL, AS_IMAGE_FROM_RES);
    as_scr_ascii_to_ucs2(IMG_TAB_MORE, IMG_TAB_MORE_D, tmp, tmp2, 30, 30);
    tab_p->add_item((as_control*)tab_p, tmp, tmp2, NULL, AS_IMAGE_FROM_RES);
    tab_p->set_bg_color((as_control*)tab_p, 0x0);
    tab_p->change_focus_idx((as_control*)tab_p, 0);
    tab_p->set_cb((as_control*)tab_p, as_scr_main_tab_evt);
    s_main_screen_p->tab = tab_p;
}

static void as_scr_set_btn_info(ASINT idx, ASSTR img_u, ASSTR img_d, ASSTR strid, btn_cb cb)
{
    s_btninfo[idx].img_u = img_u;
    s_btninfo[idx].img_d = img_d;
    s_btninfo[idx].strid = strid;
    s_btninfo[idx].cb = cb;
}
static void as_scr_init_btns(void)
{
    as_scr_set_btn_info(0, IMG_HOME_CATEGORY, IMG_HOME_CATEGORY_D, IMG_T_CATEGORY, as_scr_main_btn_category);
    as_scr_set_btn_info(1, IMG_HOME_ALL, IMG_HOME_ALL_D, IMG_T_ALL, as_scr_main_btn_all);
    as_scr_set_btn_info(2, IMG_HOME_FAVORITE, IMG_HOME_FAVORITE_D, IMG_T_FAVORITE, as_scr_main_btn_favorite);
    as_scr_set_btn_info(3, IMG_HOME_TODO, IMG_HOME_TODO_D, IMG_T_TODO, as_scr_main_btn_todo);
    as_scr_set_btn_info(4, IMG_HOME_UNTRIED, IMG_HOME_UNTRIED_D, IMG_T_UNTRIED, as_scr_main_btn_untried);
    as_scr_set_btn_info(5, IMG_HOME_TRIED, IMG_HOME_TRIED_D, IMG_T_TRIED, as_scr_main_btn_tried);
    as_scr_set_btn_info(6, IMG_HOME_MOOD, IMG_HOME_MOOD_D, IMG_T_MOOD, as_scr_main_btn_mood);
    as_scr_set_btn_info(7, IMG_HOME_RECENT, IMG_HOME_RECENT_D, IMG_T_RECENT, as_scr_main_btn_recent);
    as_scr_set_btn_info(8, IMG_HOME_RANDOM, IMG_HOME_RANDOM_D, IMG_T_RANDOM, as_scr_main_btn_random);
}
static void as_scr_add_menu(as_screen* screen_p)
{
#define SIDE_GAP 20
    as_rect area;
    as_button* bt_temp;
    ASINT w, h, intervalx, intervaly, i;
    ASUINT param[6];
    ASWCHAR tmp[30], tmp2[30];
    as_get_internal_img_res_size(IMG_HOME_CATEGORY, &w, &h);
    as_scr_init_btns();
    intervalx = (as_get_lcd_width() - SIDE_GAP * 2 - w * 3) / 2;
    intervaly = (as_get_lcd_height() - s_main_screen_p->head_height - h * 3 - BUTTON_BAR_H) / 3;

    for (i = 0; i < 9; i++)
    {
        area.x = SIDE_GAP + (intervalx + w) * (i % 3);
        area.y = s_main_screen_p->head_height + (intervaly + h) * (i / 3);
        area.w = w;
        area.h = h;
        as_scr_ascii_to_ucs2(s_btninfo[i].img_u, s_btninfo[i].img_d, tmp, tmp2, 30, 30);
        param[0] = AS_IMAGE_BUTTON;
        param[1] = (ASUINT)tmp;
        param[2] = (ASUINT)tmp2;
        param[3] = (ASUINT)NULL;
        param[4] = AS_IMAGE_FROM_RES;
        bt_temp = (as_button*)screen_p->add_ctrl(screen_p, AS_CTRL_BUTTON, area, 5, (void**)param);
        bt_temp->set_cb_by_status((as_control*)bt_temp, AS_BUTTON_UP, s_btninfo[i].cb);
        s_btns[i] = bt_temp;
        //as_get_internal_img_res_size(s_btninfo[i].strid, &w, &h);
        area.y += IMG_HOME_ALL_H;
        //area.w = w;
        //area.h = h;
        param[0] = AS_IMAGE_FROM_RES;
        param[1] = (ASUINT)as_ucs2_from_ascii(s_btninfo[i].strid);
        param[2] = ASFALSE;
        screen_p->add_ctrl(screen_p, AS_CTRL_IMAGE, area, 3, (void**)param);
    }

    s_btns[s_cur_category]->change_status((as_control*)s_btns[s_cur_category], AS_BUTTON_DOWN);
}

static ASBOOL as_scr_main_foucus_evt(as_screen* screen_p, ASINT event, ASINT keycode)
{
    ASINT isel = s_cur_category;

    if (keycode == AS_KEY_UP || keycode == AS_KEY_VUP || keycode == AS_KEY_DOWN || keycode == AS_KEY_VDOWN)
    {
        if (event == AS_EVT_KEY_UP)
        {
            if (keycode == AS_KEY_UP || keycode == AS_KEY_VUP)
            {
                s_btns[isel]->reset_status((as_control*)s_btns[isel]);
                isel--;

                if (isel < 0)
                    isel = (isel + ENUM_CATEGORY_COUNT) % ENUM_CATEGORY_COUNT;

                s_cur_category = isel;
                s_btns[isel]->change_status((as_control*)s_btns[isel], AS_BUTTON_DOWN);
            }
            else
            {
                s_btns[isel]->reset_status((as_control*)s_btns[isel]);
                isel++;

                if (isel >= ENUM_CATEGORY_COUNT)
                    isel = isel % ENUM_CATEGORY_COUNT;

                s_cur_category = isel;
                s_btns[isel]->change_status((as_control*)s_btns[isel], AS_BUTTON_DOWN);
            }

            return ASTRUE;
        }
        else
            return ASFALSE;
    }
    else
        return ASFALSE;
}
static void as_scr_main_rsk_evt(void)
{
    as_show_exit();
}

static void as_scr_main_lsk_evt(void)
{
    if (s_cur_category >= 0 && s_cur_category < ENUM_CATEGORY_COUNT)
        s_btninfo[s_cur_category].cb();
}

static void as_scr_main_create_softkey(as_screen* screen_p)
{
    as_screen_create_softkey_bar(screen_p, TEXT_ENTER, TEXT_EXIT, as_scr_main_lsk_evt, as_scr_main_rsk_evt);
}

static void as_scr_main_create_advbar(as_screen* screen_p, as_rect area)
{
    as_screen_add_advbar(screen_p, area);
}
void as_scr_main_init(as_screen* screen_p)
{
    as_rect area;
    s_main_screen_p = (as_main_screen*)screen_p;
    screen_p->set_bg_color(screen_p, 0xAE1C1C);
    as_scr_add_head(screen_p);
    area.x = 0;
    area.y = 0;
    area.w = as_get_lcd_width();
    area.h = as_app_get_advbar_height();
    as_scr_main_create_advbar(screen_p, area);
    as_scr_add_menu(screen_p);
    as_scr_main_create_softkey(screen_p);
    screen_p->set_focus_hdlr(screen_p, as_scr_main_foucus_evt);
}

void as_scr_main_deinit(as_screen* screen_p)
{
    AS_UNUSED(screen_p);
}

void as_scr_main_show(void)
{
    as_screen_manager* scr_mgr = as_get_screen_stack_instance();
    as_screen* scr_p;
    as_ret ret;
    ret = scr_mgr->new_and_push_screen(AS_SCREEN_MAIN);
    scr_p = scr_mgr->get_cur_screen();

    if (scr_p)
    {
        scr_p->paint_hdlr(scr_p);
        scr_p->flush_hdlr(scr_p);
    }
}